package com.qf.gh.web.servlet;

import com.qf.gh.domain.GoodsType;
import com.qf.gh.domain.Order;
import com.qf.gh.domain.User;
import com.qf.gh.service.OrderService;
import com.qf.gh.service.UserService;
import com.qf.gh.service.impl.OrderServiceImpl;
import com.qf.gh.service.impl.UserServiceImpl;
import com.qf.gh.utils.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/adminorder")
public class AdminOrder extends BaseServlet{
    OrderService orderService=new OrderServiceImpl();
    UserService userService=new UserServiceImpl();
    public String getAllOrder(HttpServletRequest request, HttpServletResponse response){
        List<Order> orderList=orderService.select();
        for (Order order : orderList) {
            User user=userService.selectById(order.getUid());
            order.setUser(user);
        }
        request.setAttribute("orderList",orderList);
        return "forward:/admin/showAllOrder.jsp";
    }
    public String update(HttpServletRequest request, HttpServletResponse response){
        String oid = request.getParameter("oid");
        orderService.update(oid);
        return "redirect:/adminorder?method=getAllOrder";
    }
    public String searchOrder(HttpServletRequest request, HttpServletResponse response)throws Exception{
        String username = request.getParameter("username");
        String status = request.getParameter("status");
        StringBuilder where=new StringBuilder("  WHERE 1=1  ");
        List<Object> parmas=new ArrayList<Object>();
        if(StrUtils.isNotEmpty(username)){
            where.append("  AND  name LIKE ?   ");
            parmas.add("%"+username+"%");
        }
        if(StrUtils.isNotEmpty(status)){
            where.append("  AND  status= ? ");
            parmas.add(Integer.parseInt(status));
        }
        List<Order> orderList=orderService.findByName(where.toString(),parmas);
        for (Order order : orderList) {
            User user=userService.selectById(order.getUid());
            order.setUser(user);
        }
        request.setAttribute("orderList",orderList);
        return "forward:/admin/showAllOrder.jsp";
    }
}
